package com.study.system.user.service.impl;

import com.study.system.user.domain.User;
import com.study.system.user.domain.UserDept;
import com.study.system.user.mapper.UserMapper;
import com.study.system.user.service.UserService;
import com.study.system.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Author 669
 * @CreateDate 2022/4/19 18:57
 * @Version 1.0
 */
public class UserServiceImpl implements UserService {

    @Override
    public int addUser(User user) {
        SqlSession session = MybatisUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        int i = mapper.addUser(user);
        session.commit();
        return i;
    }

    @Override
    public int updateUser(User user) {
        SqlSession session = MybatisUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        int i = mapper.updateUser(user);
        session.commit();
        return i;
    }

    @Override
    public int delUserByuserName(String username) {
        SqlSession session = MybatisUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        int i = mapper.delUserByuserName(username);
        session.commit();
        return i;
    }

    @Override
    public int delUserById(int id) {
        SqlSession session = MybatisUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        int i = mapper.delUserById(id);
        session.commit();
        return i;
    }

    @Override
    public User selectByUsername(String username) {
        SqlSession session = MybatisUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = mapper.selectByUsername(username);
        return user;
    }

    @Override
    public User selectByUsernameAndPassword(String username, String password) {
        SqlSession session = MybatisUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = mapper.selectByUsernameAndPassword(username, password);
        return user;
    }

    @Override
    public List<UserDept> selectallUserAndDept() {
        SqlSession session = MybatisUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<UserDept> userDeptList = mapper.selectallUserAndDept();
        return userDeptList;
    }

    @Override
    public UserDept selectallUserAndDeptByUsername(String username) {
        SqlSession session = MybatisUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        UserDept userDept = mapper.selectallUserAndDeptByUsername(username);
        return userDept;
    }

    @Override
    public UserDept selectallUserAndDeptById(int id) {
        SqlSession session = MybatisUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        UserDept userDept = mapper.selectallUserAndDeptById(id);
        return userDept;
    }
}
